/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanzhenjie.andserver.slice;

import com.yanzhenjie.andserver.event.MessageEvent;
import com.yanzhenjie.andserver.model.fileItemModel;
import com.yanzhenjie.andserver.provider.ListProvider;
import com.yanzhenjie.andserver.util.FileUtils;
import com.yanzhenjie.hos.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class ListAbilitySlice extends AbilitySlice {

    private ListContainer list;

    private String substring;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_list);
        initView();
        initData();
    }

    private void initData() {
        MainAbilitySlice mainAbilitySlice = new MainAbilitySlice();
        List<File> fileList = FileUtils.getFileList(mainAbilitySlice.getPath().substring(0, mainAbilitySlice.getPath().length() - 5) + "abc");
        List<fileItemModel> arrayList = new ArrayList<>();
        for (int i = 0; i < fileList.size(); i++) {

            String name = fileList.get(i).getName();
            if (name.contains(".")) {
                substring = name.substring(name.indexOf("."));
            } else {
                substring = "";
            }
            arrayList.add(new fileItemModel(fileList.get(i).getName(), "fileList.get(i).get", fileList.get(i).getAbsolutePath(), fileList.get(i).length(), substring));
        }
        ListProvider listProvider = new ListProvider(arrayList, getContext());
        list.setItemProvider(listProvider);
        list.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                try {
                    FileUtils.copy(new File(arrayList.get(i).getFilepath()), new File(mainAbilitySlice.getPath() + "/" + arrayList.get(i).getFilename()));

                } catch (Exception e) {
                    e.fillInStackTrace();
                }
                EventBus.getDefault().post(new MessageEvent());
                terminate();

            }
        });

    }

    private void initView() {
        list = (ListContainer) findComponentById(ResourceTable.Id_list);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
